package org.rsbot.script.randoms;

import java.util.logging.Logger;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(authors={"PwnZ", "Taha", "Zenzie"}, name="Mime", version=1.3D)
public class Mime extends Random
{
  private int animation;
  private RSNPC mime;

  public boolean activateCondition()
  {
    RSNPC localRSNPC = this.npcs.getNearest(new int[] { 1056 });
    return (localRSNPC != null) && (this.calc.distanceTo(localRSNPC.getLocation()) < 15);
  }

  private boolean clickAnimation(String paramString) {
    if (!this.interfaces.get(188).isValid())
      return false;
    for (int i = 0; i < this.interfaces.get(188).getChildCount(); i++) {
      if (this.interfaces.get(188).getComponent(i).getText().contains(paramString)) {
        log("Clicked on: " + paramString);
        return this.interfaces.get(188).getComponent(i).doClick();
      }
    }
    return false;
  }

  private RSNPC getNPCAt(RSTile paramRSTile) {
    for (RSNPC localRSNPC : this.npcs.getAll()) {
      if (localRSNPC.getLocation().equals(paramRSTile)) {
        return localRSNPC;
      }
    }
    return null;
  }

  private Stage getStage() {
    if ((this.interfaces.canContinue()) && (getMyPlayer().getLocation().equals(new RSTile(2008, 4764))))
      return Stage.click;
    if (this.mime == null)
      return Stage.findMime;
    if (((this.interfaces.get(372).getComponent(2).getText().contains("Watch")) || (this.interfaces.get(372).getComponent(3).getText().contains("Watch"))) && (this.mime.getAnimation() != -1) && (this.mime.getAnimation() != 858))
      return Stage.findAnimation;
    if (this.interfaces.get(188).isValid()) {
      return Stage.clickAnimation;
    }
    return Stage.wait;
  }

  public int loop()
  {
    if (!activateCondition())
      return -1;
    switch (1.$SwitchMap$org$rsbot$script$randoms$Mime$Stage[getStage().ordinal()]) {
    case 1:
      this.interfaces.clickContinue();
      sleep(random(1500, 2000));
      return random(200, 400);
    case 2:
      if (((this.mime = this.npcs.getNearest(new int[] { 1056 })) == null) && ((this.mime = getNPCAt(new RSTile(2011, 4762))) == null)) {
        this.log.warning("ERROR: Mime not found!");
        return -1;
      }
      return random(200, 400);
    case 3:
      this.animation = this.mime.getAnimation();
      this.log.info("Found Mime animation: " + this.animation);
      sleep(1000);
      if (this.interfaces.get(188).isValid())
        return random(400, 800);
      long l = System.currentTimeMillis();
      while (System.currentTimeMillis() - l >= 5000L) {
        if (this.interfaces.get(188).isValid())
          return random(1000, 1600);
        sleep(random(1000, 1500));
      }
      return random(200, 400);
    case 4:
      this.log.info("Clicking text according to animation: " + this.animation);
      if ((this.animation == -1) || (this.animation == 858)) break;
      switch (this.animation) {
      case 857:
        clickAnimation("Think");
        break;
      case 860:
        clickAnimation("Cry");
        break;
      case 861:
        clickAnimation("Laugh");
        break;
      case 866:
        clickAnimation("Dance");
        break;
      case 1128:
        clickAnimation("Glass Wall");
        break;
      case 1129:
        clickAnimation("Lean");
        break;
      case 1130:
        clickAnimation("Rope");
        break;
      case 1131:
        clickAnimation("Glass Box");
        break;
      default:
        this.log.info("Unknown Animation: " + this.animation + " Please inform a developer at RSBot.org!");
        return random(2000, 3000);
      }

    case 5:
      return random(200, 400);
    }
    return random(200, 400);
  }

  private static enum Stage
  {
    click, findMime, findAnimation, clickAnimation, wait;
  }
}